package us.hootpic.server;

import us.hootpic.client.blobs.BlobData;
import us.hootpic.client.blobs.BlobDataFilter;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;


@SuppressWarnings("serial")
public class RpcServiceImpl extends RemoteServiceServlet implements 	us.hootpic.client.rpc.RpcService {

    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 
    public String getBlobStoreUrl() {
            String url = blobstoreService.createUploadUrl("/hootpic/upload");
            return url;
    }
    
    public BlobData[] getBlobs(BlobDataFilter filter) {
      
      BlobInfoJdo db = new BlobInfoJdo();
      BlobData[] r = db.getBlobs(filter);
      
      return r;
    }
    
    public boolean deleteBlob(BlobDataFilter filter) {
      
      String blobKey = filter.getBlobKey();
      
      if (blobKey == null) {
        return false;
      }
      
      BlobKey blobKeys = new BlobKey(blobKey);
      blobstoreService.delete(blobKeys);
      
      return true;
    }

	private void test2() {

		DatastoreService datastore = DatastoreServiceFactory	.getDatastoreService();
		Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity();
		Long totalBytes = null;
		Long totalEntities = null;
		if (globalStat != null) {
			totalBytes = (Long) globalStat.getProperty("bytes");
			totalEntities = (Long) globalStat.getProperty("count");
		}

		System.out.println("totalBytes: " + totalBytes + " totalEntities: " + totalEntities);
	}

}
